import Taro from '@tarojs/taro';
import request from '../service/api'

const delay = (time) => new Promise((resolve) => setTimeout(resolve, time || 0))
export default {
  namespace: 'wallet',
  state: {
    balance: 0.00,
    refund: 0,
    payModalOpen: false,
    activeCode: '',
    fleetList: [],
    currentFleetAccount: {}
  },
  effects: {
    * init() {

    },
    // 获取用户余额
    * getBalance(_, {call, put}) {
      const {
        statusCode,
        data: {code, data: {amount = 0, point = 0, pointList = [], phoneNum, refund = ''} = {}}
      } = yield call(() => request.get('/system/order-pay/getUserInfo'));
      console.log('获取用户余额', amount);
      console.log('当前积分point', point);
      if (statusCode === 200 && code === 200) {
        yield put({
          type: 'setState',
          payload: {
            balance: parseFloat(amount),
            integral: parseFloat(point),
            pointList,
            refund
          }
        });
        yield put({
          type: 'app/upState',
          payload: {
            phoneNumberInStore: phoneNum
          }
        });
        yield put({
          type: 'getFleetList',
          payload: {
            fleetPhoneNumber: phoneNum,
            pageNum: 1,
            pageSize: 999
          }
        })
      } else {
        yield put({
          type: 'setState',
          payload: {
            balance: 0
          }
        })
      }
    },
    * getPoint2Money(_, {call, put}) {
      const {data: {data = [], code}} = yield call(() => request.get('/mini/order-pay/point2money'));
      console.log('getPoint2Money', data)
      if (code && code === 200) {
        yield put({
          type: 'setState',
          payload: {
            chargeOptions: data.length ? data.map((i) => {
              return {
                value: parseFloat(i.dictLabel),
                text: `${i.dictValue}积分`,
                money: `¥${i.dictLabel}`,
              }
            }) : []
          }
        })
      }
    },
    //查询当前登录用户是否是车队管理人员.
    * getFleetList({payload}, {call, put}) {
      console.log('getFleetList', payload);
      const {
        statusCode,
        data: {data, code} = {},
      } = yield call(() => request.get('/mini/other/fleetAndAccountInfoList', {phoneNumber: payload.fleetPhoneNumber}));
      console.log('车队信息', data);
      if (statusCode === 200 && code === 200) {
        yield put({
          type: 'setState',
          payload: {
            fleetList: data,
            isTeamLeader: !!data.length
          }
        })
      }
    },
    // 获取是否有正在启动的单子
    * findUserLoadingOrder(_, {call, put, select}) {
      const {action, countDownTime, currentGun, currentOrderNum} = yield select(({stations}) => stations);
      const res = yield call(() => request.get('/system/order/findUserLoadingOrder'));
      console.log('否有正在启动的单子回调', res);
    },
    *setAutoRefunds({payload}, {call, put, select}){
      const {statusCode, errMsg = ''} = yield call(() => request.post('/weChat/other/automatic/refunds', {status: payload.status}));
      // console.log('setAutoRefunds', data);
      if (statusCode && statusCode === 200){
        yield call(() => delay(500));
        Taro.showToast({
          title: payload.status === 0 ? '关闭成功' : '开启成功',
          icon: 'none'
        });
        yield put({
          type: 'getBalance'
        })
      } else {
        yield call(() => delay(500));
        Taro.showToast({
          title: errMsg,
          icon: 'none'
        })
      }
    }
  },
  reducers: {
    setState(state, {payload}) {
      return {
        ...state,
        ...payload
      }
    }
  }
}
